package com.example.baselib.utils;

import com.example.baselib.utils.log.LogUtils;
import ohos.app.AbilityContext;
import ohos.bundle.IBundleManager;

import java.util.ArrayList;
import java.util.List;

public class PermissionUtils {

    /**
     * 校验传入的权限有木有被用户禁止的
     *
     * @param context
     * @param permissions
     * @return 空 表示 没有,非空 则是被用户禁止的权限
     */
    public static String[] prohibitPermission(AbilityContext context, String... permissions) {
        String prohibitPermission;
        List<String> arrays = new ArrayList<>();
        for (String permission : permissions) {
            prohibitPermission = prohibitPermission(context, permission);
            if (prohibitPermission != null) {
                arrays.add(prohibitPermission);
            }
        }
        if (arrays.size() == 0) {
            return null;
        }
        return arrays.toArray(new String[arrays.size()]);
    }

    public static String prohibitPermission(AbilityContext context, String permission) {
        if (permission == null || permission.length() == 0) {
            LogUtils.w("传入的权限信息不能为空");
            return null;
        }

        if (context == null) {
            LogUtils.w("context 不能为空");
            return null;
        }
        //true允许弹框，false不允许弹框
        boolean bool = context.canRequestPermission(permission);
        LogUtils.d("权限弹框状态:%s,权限:%s", bool ? "允许" : "不允许", permission);
        if (!bool) {
            return permission;
        }
        return null;
    }

    /**
     * 是否已经获取权限
     *
     * @param context
     * @param permissions
     * @return true 已经有权限,否则没有
     */
    public static boolean hasPermission(AbilityContext context, String... permissions) {
        return verifyPermission(context, permissions) == null;
    }

    /**
     * 判断当前所以权限是否已经授权
     *
     * @param context     AbilityContext
     * @param permissions 权限组
     * @return 空 表示已经全部授权, 非空 表示所以未授权的权限
     */
    public static String[] verifyPermission(AbilityContext context, String... permissions) {
        String deniedPermission;
        List<String> arrays = new ArrayList<>();
        for (String permission : permissions) {
            deniedPermission = verifyPermission(context, permission);
            if (null != deniedPermission) {
                arrays.add(deniedPermission);
            }
        }
        if (arrays.size() == 0) {
            return null;
        }
        return arrays.toArray(new String[arrays.size()]);
    }

    /**
     * 判断当前权限是否已经授权
     *
     * @param context    AbilityContext
     * @param permission 需要请求的权限
     * @return 空 表示已经授权, 非空 表示未授权
     */
    public static String verifyPermission(AbilityContext context, String permission) {
        if (permission == null || permission.length() == 0) {
            LogUtils.w("传入的权限信息不能为空");
            return null;
        }
        if (context == null) {
            LogUtils.w("context 不能为空");
            return null;
        }
        int status = context.verifyCallingPermission(permission);
        LogUtils.d("权限状态:%s,权限:%s", status, permission);
        if (status == IBundleManager.PERMISSION_DENIED) {
            return permission;
        }
        return null;
    }


}
